package com.prs.crm.action.personalset;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;

import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.personalset.DefaultCity;
import com.prs.crm.domain.personalset.DefaultCitySet;
import com.prs.crm.domain.personalset.DefaultProvince;
import com.prs.crm.service.personalSet.DefaultCityService;
import com.prs.crm.service.personalSet.DefaultCitySetService;
import com.prs.crm.service.personalSet.DefaultProvinceService;

public class DefaultCitySetAction extends BaseAction{

	private static final int FILE_SIZE=16*1024;
	private Integer proId;
	private DefaultCitySet defaultCitySet;
	private DefaultCitySetService defaultCitySetService;
	private List<DefaultProvince> defaultProvince;
	private DefaultProvinceService defaultProvinceService;
	private List<DefaultCity> defaultCity;
	private DefaultCityService defaultCityService;
	private String serCode;
	private File upload;
    private String uploadFileName;
    private String uploadContentType;
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getSerCode() {
		return serCode;
	}
	public void setSerCode(String serCode) {
		this.serCode = serCode;
	}
	public Integer getProId() {
		return proId;
	}
	public void setProId(Integer proId) {
		this.proId = proId;
	}
	public DefaultCitySet getDefaultCitySet() {
		return defaultCitySet;
	}
	public void setDefaultCitySet(DefaultCitySet defaultCitySet) {
		this.defaultCitySet = defaultCitySet;
	}
	public DefaultCitySetService getDefaultCitySetService() {
		return defaultCitySetService;
	}
	public void setDefaultCitySetService(DefaultCitySetService defaultCitySetService) {
		this.defaultCitySetService = defaultCitySetService;
	}
	public List<DefaultProvince> getDefaultProvince() {
		return defaultProvince;
	}
	public void setDefaultProvince(List<DefaultProvince> defaultProvince) {
		this.defaultProvince = defaultProvince;
	}
	public DefaultProvinceService getDefaultProvinceService() {
		return defaultProvinceService;
	}
	public void setDefaultProvinceService(
			DefaultProvinceService defaultProvinceService) {
		this.defaultProvinceService = defaultProvinceService;
	}

	public List<DefaultCity> getDefaultCity() {
		return defaultCity;
	}
	public void setDefaultCity(List<DefaultCity> defaultCity) {
		this.defaultCity = defaultCity;
	}
	public DefaultCityService getDefaultCityService() {
		return defaultCityService;
	}
	public void setDefaultCityService(DefaultCityService defaultCityService) {
		this.defaultCityService = defaultCityService;
	}
	
	@Action("editDefaultCitySet")
	public String editDefaultCitySet(){
		this.defaultProvince=this.getDefaultProvinceService().getAllDefaultProvince();
//		this.defaultCity=this.getDefaultCityService().getAllDefaultCity();
		this.defaultCitySet=this.getDefaultCitySetService().getDefaultCitySet(serCode);
		return SUCCESS;
	}
	
	@Action(value="ajaxDefaultCitySet",results = { @Result(name = "success", type = "json",
			params = {"root", "defaultCity" }) })
	public String ajaxDefaultCitySet(){
		this.defaultCity=this.getDefaultCityService().getAllDefaultCityById(proId);
		return SUCCESS;
	}
	@Action(value="saveDefaultCitySet",results={
			@Result(name="success",type="redirect",location="editDefaultCitySet?serCode=${defaultCitySet.getType()}"),
			@Result(name="error",type="chain",location="editDefaultCitySet?serCode=${defaultCitySet.getType()")
	})
	public String saveDefaultCitySet(){
		DefaultCitySet dc=this.getDefaultCitySetService().getDefaultCitySet("dc01");
		if(dc==null){
			dc=new DefaultCitySet();
			dc.setType("dc01");
			dc.setDefaultProvince(defaultCitySet.getDefaultProvince());
			dc.setDefaultCity(defaultCitySet.getDefaultCity());
			this.getDefaultCitySetService().save(dc);
		}else{
			this.getDefaultCitySetService().submit(dc);
		}
		defaultCitySet=dc;
		return SUCCESS;
	}
	
//	
//    @Action("edituploads")
//    public String edituploads(){
// 	   return SUCCESS;
//    }
    
//	@Action(value = "saveupload", results = {
//			@Result(name = "success", type = "redirect", location = "edituploads?defaultCitySet=${defaultCitySet.getId()}"),
//			@Result(name = "error", type = "chain", location = "edituploads?defaultCitySet=${defaultCitySet.getId()}") })
//    public String saveupload() throws IOException{
////			defaultCitySet=new DefaultCitySet();
////			String path=ServletActionContext.getServletContext().getRealPath("upload")
////			  +"/"+this.imageFileName;
////			  System.out.println(path);
////			  File targetFile=new File(path);
////			  upLoadFile(upload,targetFile);
//			System.out.println("文件名："+uploadFileName);
//			this.defaultCitySet.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
////			defaultCitySet.setImageFileName(imageFileName);
////			defaultCitySet.setImageContentType(path);
////			defaultCitySet.setImageFileName(FilenameUtils.getName(targetFile.getPath()));
//           this.getDefaultCitySetService().save(defaultCitySet);
//           return SUCCESS;
//    }
//	
//	public void upLoadFile(File source,File target){
//		  InputStream in=null;
//		  OutputStream out=null;
//		  try{
//		   in=new BufferedInputStream(new FileInputStream(source),FILE_SIZE);
//		   out=new BufferedOutputStream(new FileOutputStream(target),FILE_SIZE);
//		   byte[] image=new byte[FILE_SIZE];
//		   while(in.read(image)>0){
//		    out.write(image);
//		   }
//		  }catch(IOException ex){
//		   ex.printStackTrace();
//		  }finally{
//		   try{
//		    in.close();
//		    out.close();
//		   }catch(IOException ex){
//		    
//		   }
//		  }
//		 }


}
